package org.jeecg.common.util.mall;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

@Slf4j
public class AccessTokenUtil {

    @Autowired
    private static RedisUtil redisUtil;


    private static Long maxExpire = 7200L * 1000;
    private static String appKey;
    private static String appSecret;
    private static String accessToken;
    private static String refreshToken;
    private static String accessTokenTime;
    private static Integer status = 0;

    private static void getResource() {
        if (appKey == null || appSecret == null) {
            appKey = SpringContextUtil.getProperty("kpl.appKey");
            appSecret = SpringContextUtil.getProperty("kpl.appSecret");
        }
    }

    public static String getAccessToken() throws Exception {
        getResource();
        log.info("getAccessToken--> token:" + accessToken + " status: " + status);
        if (status == 0) {
            accessToken = (String) redisUtil.get(appKey);
            if (accessToken == null) {
                accessToken = SpringContextUtil.getProperty("kpl.access_token");
                refreshToken = SpringContextUtil.getProperty("kpl.refresh_token");
                maxExpire = Long.parseLong(SpringContextUtil.getProperty("kpl.expire"));
                accessTokenTime = SpringContextUtil.getProperty("kpl.token_time");
                redisUtil.set(appKey, accessToken);
                redisUtil.set(appKey + "-refresh", refreshToken);
                redisUtil.set(appKey + "-time", accessTokenTime);
                redisUtil.set(appKey + "-expire", maxExpire + "");
            } else {
                accessTokenTime = (String) redisUtil.get(appKey + "-time");
                refreshToken = (String) redisUtil.get(appKey + "-refresh");
                String expire = (String) redisUtil.get(appKey + "-expire");
                if (expire != null) {
                    maxExpire = Long.parseLong(expire);
                }
            }
            status = 1;
        } else {
            if (System.currentTimeMillis() - Long.parseLong(accessTokenTime) > (maxExpire * 1000)) {
                refreshToken(refreshToken);
            }
        }
        return accessToken;
    }

    private static void refreshToken(String refresh) throws Exception {
        String url = "https://kploauth.jd.com/oauth/token?grant_type=oauth_refresh_token&app_key=" + appKey + "&app_secret=" + appSecret + "&refresh_token=" + refresh;
        String result = HttpUtils.httpClientGet(url, "");

        log.info("refreshAccessToken--> res:" + result);

        Map<String, Object> resObj = JSONUtil.parseObj(result);

        if (resObj.containsKey("access_token")) {
            accessToken = resObj.get("access_token").toString();
            maxExpire = Long.parseLong(resObj.get("expires_in").toString());
            accessTokenTime = resObj.get("time").toString();
            refreshToken = resObj.get("refresh_token").toString();
            redisUtil.set(appKey, accessToken);
            redisUtil.set(appKey + "-refresh", refreshToken);
            redisUtil.set(appKey + "-time", accessTokenTime);
            redisUtil.set(appKey + "-expire", maxExpire + "");
        }
    }

    public static String getAppKey() {
        getResource();
        return appKey;
    }

    public static String getAppSecret() {
        getResource();
        return appSecret;
    }

}